{template 'cloud/header'}
<script type="text/javascript">
<!--
	var packet = {php echo ($packet ? json_encode($packet) : 'null');};
	angular.module('cloudApp');
	angular.module('cloudApp')
	.controller('FileProcessorCtrl', function($scope, $http, config) {
		$scope.files = config.files;
		$scope.fails = [];

		var total = $scope.files.length;
		var i = 1;
		var errormsg = '';
		var tasknum = config.tasknum && config.type != '' ? config.tasknum : 1;
		if (tasknum > total) {
			tasknum = 1;
		}
		var proc = function() {
			var path = $scope.files.pop();
			if(!path && i >= total) {
				if ($scope.fails && $scope.fails.length > 0) {
					util.message('文件同步失败，可能是目录权限问题，请重试同步成功后继续。', './index.php?c=cloud&a=upgrade&', 'info');
				} else {
					var targetUrl = '';
					if (config.type == 'theme') {
						targetUrl = './index.php?c=cloud&a=process&step=schemas&t=' + config.appname + '&is_upgrade=' + config.is_upgrade;
					} else if (config.type == 'webtheme') {
						targetUrl = './index.php?c=cloud&a=process&step=schemas&w=' + config.appname + '&is_upgrade=' + config.is_upgrade;
					} else {
						targetUrl = './index.php?c=cloud&a=process&step=schemas&m=' + config.appname + '&is_upgrade=' + config.is_upgrade + '&batch=1&account_type=' + config.account_type;
					}
					util.message('文件同步完成，正在处理数据同步......', targetUrl, 'success');
				}
				return;
			}
			$scope.file = path;
			$scope.pragress = i + '/' + total;
			var params = {path: path, type : config.type};
			$http.post(location.href, params).success(function(dat){
				i++;
				if(dat != 'success') {
					$scope.fails.push('['+dat+'] ' + path);
					errormsg = dat;
				}
				proc();
			}).error(function(){
				i++;
				$scope.fails.push(path);
				proc();
			});
		}
		for (j = 0; j < tasknum; j++) {
			proc();
		}
	})
	.controller('SchemasProcessorCtrl', function($scope, $http, config){
		if (packet && (packet.files.length > 0)) {
			util.message('请先完成文件同步', './index.php?c=cloud&a=upgrade');
			return false;
		}
		$scope.schemas = config.schemas;
		$scope.fails = [];
		var is_m_install = config.is_module_install;
		var total = $scope.schemas.length;
		var i = 1;
		var error = function() {
			util.message('未能成功执行处理数据库, 请联系开发商解决. ');
		}
		var proc = function() {
			var schema = $scope.schemas.pop();
			if(!schema) {
				if($scope.fails.length > 0) {
					error();
					return;
				} else {
					if(is_m_install == 1) {
						location.href = '';
					} else {
						location.href = '';
					}
					return;
				}
			}
			$scope.schema = schema;
			$scope.pragress = i + '/' + total;
			var params = {table: schema};
			$http.post(location.href, params).success(function(dat){
				i++;
				if(dat != 'success') {
					$scope.fails.push(schema)
				}
				if (dat['message']) {
					util.message(dat['message']);
					return;
				}
				proc();
			}).error(function(){
				i++;
				$scope.fails.push(schema);
				proc();
			});
		}
		proc();
	})
	.controller('CloudDiagnoseCtrl', ['$scope', '$http', 'config', function($scope, $http, config){
		$scope.showToken = function() {
			util.message('Token:' + $('#token').val(), '', 'info');
		};

		$('.js-checkip p').each(function(){
			var $this = $(this);
			$.getJSON('./index.php?c=cloud&a=diagnose&do=testapi&ip='+$this.find('#serverdnsip').html(), function(testdata) {
				$this.find('#checkresult').html(testdata.message.message);
			});
		});
		$.ajax({
			type : "get",
			data : {'date' : config.date, 'version' : config.version, 'siteurl' : config.siteurl},
			url : "https://v2.addons.we7.cc/web/index.php?c=site&a=diagnose&jsonpcallback=?",
			dataType : "jsonp",
			success : function(data){
				if (data['check_time']['errno'] == '0') {
					$('#check-time').html('<i class="fa fa-check text-success"></i> 正常');
				} else {
					$('#check-time').html('<i class="fa fa-remove text-warning"></i> 异常，当前时间为：'+data['check_time']['message']['localtime']+"; 服务器时间为："+data['check_time']['message']['servertime']);
				}
				if (data['check_touch']['errno'] == '0') {
					$('#check-touch').html('<i class="fa fa-check text-success"></i> 正常');
				} else {
					$('#check-touch').html('<i class="fa fa-remove text-warning"></i> 异常，'+data['check_touch']['message']);
				}
			},
			error:function(){
				alert('fail');
			}
		});
	}]).controller('processor', function($scope, $http){
		if (packet && (packet.files.length > 0 || packet.files.schemas > 0)) {
			util.message('请先完成文件或是数据库同步', './index.php?c=cloud&a=upgrade');
			return false;
		}
		$scope.scripts = {php echo json_encode($scripts);};
		$scope.fails = [];

		var is_upgrade = "{php echo $is_upgrade}";
		var total = $scope.scripts.length;
		var i = 1;
		var error = function() {
			util.message('未能成功执行清理升级操作, 请联系开发者. ');
		}
		var proc = function() {
			var script = $scope.scripts.shift();
			if(!script) {
				if($scope.fails.length > 0) {
					error();
				} else {
					if(is_upgrade == 1) {
						{if $_GPC['batch']}
							location.href = '{url "module/manage-system/upgrade" array("m" => $m, "flag" => 1, "batch_from_cloud" => 1)}';
						{else}
							location.href = '{url "module/manage-system/upgrade" array("m" => $m, "flag" => 1)}';
						{/if}
						return;
					}

					var is_confirm = "{php echo $_GPC['is_confirm']}";
					if(is_confirm == 1) {
						if(confirm('已经成功执行升级操作! '+"\n"+' 由于数据库更新, 可能会产生多余的字段. 你可以按照需要删除')) {
							location.href = '{url "cloud/upgrade"}';
						} else {
							location.href = '{url "cloud/upgrade"}';
						}
					} else {
						util.message('已经成功执行升级操作! '+"\n"+' 由于数据库更新, 可能会产生多余的字段. 你可以按照需要删除.', '{url "cloud/upgrade"}');
					}
					return;
				}
			}
			$scope.script = script.fname;
			$scope.message = script.message;
			$scope.pragress = i + '/' + total;
			var params = {fname: script.fname};
			$http.post(location.href, params).success(function(dat){
				i++;
				if(dat != 'success') {
					$scope.fails.push(script.fname)
					error();
					return;
				}
				proc();
			}).error(function(){
				i++;
				$scope.fails.push(script.fname);
				error();
			});
		}
		proc();
	});
//-->
</script>
<div class="panel panel-cut">
	<div class="panel-heading">
		<i class="wi wi-upgrade" style="font-size: 24px; margin-right: 10px; vertical-align:middle;"></i>
		正在升级
	</div>
	<div class="panel-body">
		{if $step == 'files'}
		{if !empty($_GPC['m']) || 1}
		<div class="steps">
			<div class="steps-item steps-status-finish">
				<div class="steps-line"><span class="steps-num">1</span></div>
				<div class="steps-state">安装应用</div>
			</div>
			<div class="steps-item steps-status-wait">
				<div class="steps-line"><span class="steps-num">2</span></div>
				<div class="steps-state">分配应用权限</div>
			</div>
			<div class="steps-item steps-status-wait">
				<div class="steps-line"><span class="steps-num">3</span></div>
				<div class="steps-state">安装成功</div>
			</div>
		</div>
		{/if}
		<div class="clearfix js-processor">
			<div class="alert alert-warning">
				正在更新系统文件, 请不要关闭窗口.
			</div>
			<div class="alert alert-warning">
				如果下载文件失败，可能造成的原因：写入失败，请仔细检查写入权限是否正确。
			</div>
			<div class="alert alert-info form-horizontal ng-cloak" ng-controller="FileProcessorCtrl">
				<dl class="dl-horizontal">
					<dt>整体进度</dt>
					<dd>{{pragress}}</dd>
					<dt>正在下载文件</dt>
					<dd>{{file}}</dd>
				</dl>
				<dl class="dl-horizontal" ng-show="fails.length > 0">
					<dt>下载失败的文件</dt>
					<dd>
						<p class="text-danger" ng-repeat="file in fails" style="margin:0;">{{file}}</p>
					</dd>
				</dl>
			</div>
			<script type="text/javascript">
				angular.module('cloudApp').value('config', {
					files : {php echo ($packet['files'] ? json_encode($packet['files']) : '[]');},
					type : '{$type}',
					appname : '{$m}',
					is_upgrade : '{$is_upgrade}',
					account_type : "{$_GPC['account_type']}",
					tasknum : '{$packet['task']}',
				});
				angular.bootstrap($('.js-processor'), ['cloudApp']);
			</script>
		</div>
		{/if}
		{if $step == 'schemas'}
		<div class="clearfix js-processor">
			{if empty($packet['schemas'])}
				<!-- 如果是安装模块,数据库操作完成后,不处理script,直接跳转到module/manage-system/install -->
				<!-- 安装 -->
				{if !empty($packet['install'])}
					{if $packet['type'] == 'theme'}
					<script>
						location.href = '{url "system/template/install" array("templateid" => $m, "flag" => 1)}';
					</script>
					{elseif $packet['type'] == 'webtheme'}
					<script>
						location.href = '{url "system/webtheme/install" array("webtheme" => $m, "flag" => 1)}';
					</script>
					{else}
					<script>
						location.href = '{url "module/manage-system/install" array("module_name" => $m, "flag" => 1, "account_type" => $_GPC["account_type"])}';
					</script>
					{/if}
				{/if}
				<!-- 升级 -->
				{if $packet['type'] == 'theme'}
				<script>
					location.href = '{url "cloud/process" array("step" => "scripts", "t" => $m, "is_upgrade" => $is_upgrade)}';
				</script>
				{elseif $packet['type'] == 'webtheme'}
				<script>
					location.href = '{url "cloud/process" array("step" => "scripts", "w" => $m, "is_upgrade" => $is_upgrade)}';
				</script>
				{else}
				<script>
					{if $_GPC['batch']}
						location.href = '{url "cloud/process" array("step" => "scripts", "m" => $m, "is_upgrade" => $is_upgrade, "batch" => 1, "account_type" => $_GPC["account_type"])}';
					{else}
						location.href = '{url "cloud/process" array("step" => "scripts", "m" => $m, "is_upgrade" => $is_upgrade, "account_type" => $_GPC["account_type"])}';
					{/if}
				</script>
				{/if}

			{/if}
			<div class="alert alert-warning">
				正在更新数据库, 请不要关闭窗口.
			</div>
			<div class="alert alert-info form-horizontal ng-cloak" ng-controller="SchemasProcessorCtrl">
				<dl class="dl-horizontal">
					<dt>整体进度</dt>
					<dd>{{pragress}}</dd>
					<dt>正在处理数据表</dt>
					<dd>{{schema}}</dd>
				</dl>
				<dl class="dl-horizontal" ng-show="fails.length > 0">
					<dt>处理失败的数据表</dt>
					<dd>
						<p class="text-danger" ng-repeat="schema in fails" style="margin:0;">{{schema}}</p>
					</dd>
				</dl>
			</div>
			<script type="text/javascript">
				angular.module('cloudApp').value('config', {
					schemas : {php echo ($schemas ? json_encode($schemas) : '[]');},
					is_module_install : {php echo (!empty($packet['install']) ? 'true' : 'false')},
				});
				angular.bootstrap($('.js-processor'), ['cloudApp']);
			</script>
		</div>
		{/if}
		{if $step == 'scripts'}
		<div class="clearfix js-processor">
			{if empty($packet['scripts']) || !empty($packet['type'])}
				<!-- 如果是更新模块,跳转到module/manage-system/upgrade -->
				{if $is_upgrade == 1}
					{if $packet['type'] == 'theme'}
					<script>
						location.href = '{url "system/template/upgrade" array("templateid" => $m, "flag" => 1)}';
					</script>
					{elseif $packet['type'] == 'webtheme'}
					<script>
						location.href = '{url "system/webtheme/upgrade" array("webthemeid" => $m, "flag" => 1)}';
					</script>
					{else}
					<script>
						location.href = '{url "module/manage-system/upgrade" array("module_name" => $m, "flag" => 1, "account_type" => $_GPC["account_type"])}';
					</script>
					{/if}

				{/if}
				<script>
					var is_confirm = "{php echo $_GPC['is_confirm']}";
					if(is_confirm == 1) {
						if(confirm('已经成功执行升级操作! '+"\n"+' 由于数据库更新, 可能会产生多余的字段. 你可以按照需要删除')) {
							location.href = '{url "cloud/upgrade"}';
						} else {
							location.href = '{url "cloud/upgrade"}';
						}
					} else {
						require(['util'], function(u){
							u.message('已经成功执行升级操作! '+"\n"+' 由于数据库更新, 可能会产生多余的字段. 你可以按照需要删除.', '{url "cloud/upgrade"}');
						});
					}
				</script>
			{/if}
			<div class="alert alert-warning">
				正在数据迁移及清理操作, 请不要关闭窗口.
			</div>
			<div class="alert alert-info form-horizontal ng-cloak" ng-controller="processor">
				<dl class="dl-horizontal">
					<dt>整体进度</dt>
					<dd>{{pragress}}</dd>
					<dt>正在处理</dt>
					<dd>{{script}}<br />{{message}}</dd>
				</dl>
				<dl class="dl-horizontal" ng-show="fails.length > 0">
					<dt>处理失败的操作</dt>
					<dd>
						<p class="text-danger" ng-repeat="script in fails" style="margin:0;">{{script}}</p>
					</dd>
				</dl>
			</div>
			<script>
				angular.bootstrap($('.js-processor'), ['cloudApp']);
			</script>
		</div>
		{/if}
	</div>
</div>
{template 'common/footer'}